package com.yunhe.day322.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.day322.domain.Member;
import com.yunhe.day322.domain.Orders;
import com.yunhe.day322.domain.Product;
import com.yunhe.day322.domain.Traveller;
import com.yunhe.day322.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private ProductService productService;

    @Autowired
    private MemberService memberService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private TravellerService travellerService;
    @Autowired
    private OrderAndTravellerService ots;
    /**
     * 查询所有订单
     */
    @RequestMapping("findAll")
    public String findAll(Model model , @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "5") int size){

        Map<String,Object> ordersList = orderService.findAll(page,size);



        model.addAttribute("pageInfo",ordersList);
        return "order-list";
    }
    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public String findById(Model model,String id){
        Orders orders = orderService.findById(id);
        model.addAttribute("order",orders);

        return "order-show";
    }


    /**
     * 新增订单，同时查询出有哪些 product，有哪些 member ,添加的游客是哪些人
     * @return
     */
    @GetMapping("add")
    public String add(Model model){

        //下拉查询 product, 在产品中已写过方法
        List<Product> productList = productService.findAll();

        //下拉查询 member
        List<Member> memberList = this.memberService.findAll();

        //下拉查询 Traveller
        List<Traveller> travellerList = travellerService.findAll();

        //显示到order-add.html
        model.addAttribute("productList",productList);
        model.addAttribute("memberList",memberList);
        model.addAttribute("travellerList",travellerList);

        return "order-add";

    }
    /**
     * 新增订单
     * @param orders
     * @return
     */
    @RequestMapping("save")
    public String save(Orders orders){

        //添加订单信息
        orderService.save(orders);

        //根据添加订单orderNum 查询订单id  select id from orders where orderNum = #{orderNum}
        String orderId = orderService.findByOrderNum(orders.getOrderNum());

        //中间表order_traveller 添加   参数1：订单id   参数2：旅客ids
        ots.add(orderId,orders.getTravellerId());

        return "redirect:findAll";
    }
      @RequestMapping("/deleteById")
    public String deleteOrdersById(Orders orders){
         orderService.deleteOrdersById(orders.getId());
         ots.deleteOtsById(orders.getId());
         return "redirect:findAll";
    }
    /**
     * 根据id查找订单,找到后共享到修改页面进行修改
     * @param
     * @return
     */
    @RequestMapping("/editById")
    public String edit(String id,Model model){
        Orders orders =  orderService.findOrderById(id);
        model.addAttribute("orders",orders);
        //下拉查询 product,
        List<Product> productList = productService.findAll();
        //下拉查询 member
        List<Member> memberList = memberService.findAll();
        //下拉查询 Traveller
        List<Traveller> travellerList = travellerService.findAll();
        //显示到order-add.html
        model.addAttribute("productList",productList);
        model.addAttribute("memberList",memberList);
        model.addAttribute("travellerList",travellerList);
        return "order-edit";
    }
    /**
     * 修改订单
     * @param
     * @return
     */
    @RequestMapping("edit")
    public String edit( Orders orders) {
        // editById?id=0E7231DC797C486290E8713CA3C6ECCC
        //订单修改业务
        System.out.println(orders);
        orderService.edit(orders);

        //根据添加订单orderNum 查询订单id
        String OrderId = orderService.findByOrderNum(orders.getOrderNum());
//        //中间表order_traveller 修改   参数1：订单id   参数2：旅客ids
        ots.update(OrderId,orders.getTravellerId());
        return "redirect:findAll";
    }

}
